<?php

class AuctionCreateOrder
{

    public $auctionId;

    public function createOrder()
    {
        $ordersModel = new Orders();
        $imagesModel = new Images();
        $auctionimagesModel = new AuctionImages();
        $id = $this->auctionId;
        $arr = AuctionInfo::model()->findByPk($id);
        $arr_fee = Yii::app()->db->createCommand('select  *    from   auction_fee   where   auction_id=' . $id)->queryAll();
        
        
        foreach ($arr_fee as $v) {
            if ($v['fee_name'] == "value_price") {
                $arr_fee_value['value_price'] = $v['fee_value'];
            } else 
                if ($v['fee_name'] == "billing_fee") {
                    $arr_fee_value['billing_fee'] = $v['fee_value'];
                } else 
                    if ($v['fee_name'] == "shipping_fee") {
                        $arr_fee_value['shipping_fee'] = $v['fee_value'];
                    }
        }
        
        
        $model_category = AuctionCategoryRelations::model()->find('auction_id=:auction_id  and   is_delete=0', array(
            ':auction_id' => $id
        ));
        $model_product = AuctionProductRelations::model()->find('auction_id=:auction_id  and   is_delete=0', array(
            ':auction_id' => $id
        ));
        $model_pool = AuctionInfo::model()->find('id=:id  ', array(
            ':id' => $id
        ));
        $arr_param = Yii::app()->db->createCommand('select  *    from   auction_parame   where   auction_id=' . $id)->queryAll();
        // 因为这里没有全有没有的情况
        $arr_param_value['drop_ship'] = "";
        $arr_param_value['allow_auto_bidder'] = "";
        $arr_param_value['allow_buy_it_now'] = "";
        $arr_param_value['meta_title'] = "";
        $arr_param_value['meta_keyword'] = "";
        $arr_param_value['url'] = "";
        $arr_param_value['meta_description'] = "";
        $arr_param_value['buyitnow_date'] = "";
        $arr_param_value['featured_home'] = "";
        // 整个ok，去掉
        foreach ($arr_param as $v) {
            if ($v['parame_name'] == "drop_ship") {
                $arr_param_value['drop_ship'] = $v['parame_value'];
            } else 
                if ($v['parame_name'] == "allow_auto_bidder") {
                    $arr_param_value['allow_auto_bidder'] = $v['parame_value'];
                } else 
                    if ($v['parame_name'] == "allow_buy_it_now") {
                        $arr_param_value['allow_buy_it_now'] = $v['parame_value'];
                    } else 
                        if ($v['parame_name'] == "meta_title") {
                            $arr_param_value['meta_title'] = $v['parame_value'];
                        } else 
                            if ($v['parame_name'] == "meta_keywords") {
                                $arr_param_value['meta_keyword'] = $v['parame_value'];
                            } else 
                                if ($v['parame_name'] == "url") {
                                    $arr_param_value['url'] = $v['parame_value'];
                                } else 
                                    if ($v['parame_name'] == "meta_description") {
                                        $arr_param_value['meta_description'] = $v['parame_value'];
                                    } else 
                                        if ($v['parame_name'] == "buyitnow_date") {
                                            $arr_param_value['buyitnow_date'] = $v['parame_value'];
                                        } else 
                                            if ($v['parame_name'] == "featured_home") {
                                                $arr_param_value['featured_home'] = $v['parame_value'];
                                            }
                                            else  if($v['parame_name']=="vendor"){
                                                    $arr_fee_value['vendor']=$v['parame_value'];
                                                }
        }
        $model_auctionbidlog = AuctionBidLog::model()->find('auction_id=:auction_id  ', array(
            ':auction_id' => $id
        ));
        
        $auctionsModel = Auctions::model()->findByPK($id);
        
        // if(count($auctionsModel)==0){
        $auctionsModel = new Auctions();
        $auctionsModel->auction_save($arr, $arr_fee_value, $model_product, $model_category, $model_pool, $arr_param_value, $model_auctionbidlog);
        // }
        $ordersModel->order_save($arr, $arr_fee_value, $model_auctionbidlog, $model_pool);
        $model_auctionresource = AuctionResource::model()->findAll('auction_id=:auction_id  ', array(
            ':auction_id' => $id
        ));
        foreach ($model_auctionresource as $m) {
            $imagesModel->isNewRecord = true;
            $auctionimagesModel->isNewRecord = true;
            $imagesModel->path = $m->images->path;
            $imagesModel->created = time();
            if ($m->images->is_delete == 0) {
                $enable = 1;
            } else {
                $enable = 0;
            }
            $imagesModel->save() && $imagesModel->id = 0;
            $auctionimagesModel->image_id = Yii::app()->db->getLastInsertID();
            $auctionimagesModel->auction_id = $id;
            $auctionimagesModel->order = 1;
            $auctionimagesModel->enable = $enable;
            $auctionimagesModel->save() && $auctionimagesModel->id = 0;
        }
    }
}